package com.avic.map.controller;

import com.avic.map.common.constant.AppConstant;
import com.avic.map.common.constant.SystemLogConstant;
import com.avic.map.common.constant.UserConstant;
import com.avic.map.common.model.enums.MapTaggingEnums;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.lang.BaseResponse;
import com.avic.map.common.util.lang.ServiceCallBack;
import com.avic.map.controller.info.MapTaggingInfo;
import com.avic.map.controller.request.CreateMapTaggingReq;
import com.avic.map.controller.request.UpdateMapTaggingReq;
import com.avic.map.controller.result.CreateMapTaggingRes;
import com.avic.map.controller.result.GatMapTaggingRes;
import com.avic.map.dal.model.MapTagging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huangsheng on 2018/12/22
 */
@Slf4j
@RestController
@RequestMapping("tagging")
@Api(tags = "地图标注", description = "标注数据的增删改查")
public class MapTaggingController extends AbstractFacadeImpl {

    @GetMapping
    @ApiOperation(value = "查询地图里面的标注数据")
    public GatMapTaggingRes gatMapTagging() {
        final GatMapTaggingRes result = new GatMapTaggingRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                List<MapTagging> uavRecordsResult = new ArrayList<>();
                List<MapTagging> wrongUavRecords = mapTaggingService.wrongUavRecords(MapTaggingEnums.UAV);
                List<MapTagging> uavRecords = mapTaggingService.uavRecords(MapTaggingEnums.UAV);
                if (null != wrongUavRecords) {
                    uavRecordsResult.addAll(wrongUavRecords);
                }
                if (null != uavRecords) {
                    uavRecordsResult.addAll(uavRecords);
                }

                List<MapTaggingInfo> mapTaggingInfos = new ArrayList<>();
                uavRecordsResult.forEach(mapTagging -> {
                    MapTaggingInfo maptagginginfo = new MapTaggingInfo();
                    maptagginginfo.setId(mapTagging.getId());
                    maptagginginfo.setLongitude(mapTagging.getLongitude());
                    maptagginginfo.setLatitude(mapTagging.getLatitude());
                    maptagginginfo.setDescribe(mapTagging.getDescribe());
                    maptagginginfo.setUavId(mapTagging.getUavId());
                    maptagginginfo.setUavRtsp(mapTagging.getUavRtsp());
                    maptagginginfo.setTaggingType(mapTagging.getTaggingType());
                    maptagginginfo.setCreateTime(StringUtils.isNotBlank(mapTagging.getCreateTime()) ?
                            StringUtils.substringBefore(mapTagging.getCreateTime(), AppConstant.MYBATIS_DATA_TIME_SEPARATOR) : StringUtils.EMPTY);
                    maptagginginfo.setUavName(mapTagging.getUavName());
                    mapTaggingInfos.add(maptagginginfo);
                });

                result.setTaggingRecords(mapTaggingInfos);
                result.setSuccess(true);
            }
        });
    }

    @PostMapping
    @ApiOperation(value = "新增地图标注")
    public CreateMapTaggingRes createMapTaggingRes(@RequestBody CreateMapTaggingReq createMapTaggingReq,
                                                   @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final CreateMapTaggingRes result = new CreateMapTaggingRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {

            }

            @Override
            public void doService() throws Exception {
                //  新增标注时，暂时不录入描述信息
                MapTagging mapTagging = new MapTagging();
                mapTagging.setLongitude(createMapTaggingReq.getLongitude());
                mapTagging.setLatitude(createMapTaggingReq.getLatitude());
                mapTagging.setTaggingType(createMapTaggingReq.getTaggingType());

                result.setTaggingId(mapTaggingService.create(mapTagging));

                MapTaggingEnums mapTaggingEnums = MapTaggingEnums.getMapTaggingEnumsByType(createMapTaggingReq.getTaggingType());
                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.CREATE_MAP_TAGGING, null != mapTaggingEnums ? mapTaggingEnums.getDescribe() : StringUtils.EMPTY,
                                result.getTaggingId(), mapTagging.getLongitude(), mapTagging.getLatitude(), objectMapper.writeValueAsString(mapTagging)), currentUserId);
                result.setSuccess(true);
            }
        });
    }

    @PutMapping
    @ApiOperation(value = "更新地图标注")
    public BaseResponse createMapTaggingRes(@RequestBody UpdateMapTaggingReq updateMapTaggingReq,
                                            @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {

            }

            @Override
            public void doService() throws Exception {
                MapTagging mapTagging = new MapTagging();
                mapTagging.setId(updateMapTaggingReq.getId());
                mapTagging.setLongitude(updateMapTaggingReq.getLongitude());
                mapTagging.setLatitude(updateMapTaggingReq.getLatitude());
                mapTagging.setDescribe(updateMapTaggingReq.getDescribe());
                mapTagging.setUavId(updateMapTaggingReq.getUavId());

                mapTaggingService.update(mapTagging);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.UPDATE_MAP_TAGGING, mapTagging.getId(),
                                mapTagging.getLongitude(), mapTagging.getLatitude(), objectMapper.writeValueAsString(mapTagging)), currentUserId);
                result.setSuccess(true);
            }
        });
    }


    @ApiOperation(value = "删除地图标注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", paramType = "path", value = "资源ID", required = true)
    })
    @DeleteMapping("/{itemId}")
    public BaseResponse deleteMapTagging(@PathVariable("itemId") int itemId,
                                         @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(itemId > 0, BizErrorCode.PARAM_ERROR, "数据ID大于零");

            }

            @Override
            public void doService() throws Exception {
                mapTaggingService.remove(itemId);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.DELETE_MAP_TAGGING, itemId), currentUserId);
                result.setSuccess(true);
            }
        });
    }

}
